package com.karson.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.dto.FileRecordDTO;
import com.karson.common.model.TrackRate;
import com.karson.common.model.UploadFile;
import com.karson.common.request.*;
import com.karson.common.response.AlbumResponse;
import com.karson.common.response.CommonResult;
import com.karson.common.response.TrackResponse;
import com.karson.common.util.AsmrUtil;
import com.karson.common.util.FileTypeUtil;
import com.karson.service.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/track")
@Tag(name = "声音管理接口")
public class TrackInfoController {
    @Autowired
    private TrackInfoService trackInfoService;
    @Autowired
    private ISimilarityService similarityService;
    @Autowired
    private TrackRateService trackRateService;
    @Autowired
    private SubtitlesService subtitlesService;


    @Operation(summary = "声音新增")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Validated List<TrackRequest> trackRequest) {
        Boolean save = trackInfoService.saveAlbumInfo(trackRequest);
        return save?CommonResult.success():CommonResult.failed("已创建歌曲列表，跳过创建");
    }
    @Operation(summary = "音频附件信息上传")
    @PostMapping("/upload/explain/{albumId}")
    public CommonResult<String> uploadExplain(@PathVariable("albumId") Integer albumId, @RequestBody @Validated List<FileRequest> files) throws Exception {
        Boolean upload = similarityService.uploadExplain(albumId,files);
        return upload?CommonResult.success():CommonResult.failed();
    }
    @Operation(summary = "根据专辑查询音声信息")
    @GetMapping("/list/{albumId}")
    public CommonResult<List<TrackResponse>> getList(@PathVariable("albumId") Integer albumId) {
        List<TrackResponse> trackResponses = trackInfoService.getTrackInfo(albumId);
        return CommonResult.success(trackResponses);
    }
    @Operation
    @DeleteMapping("/del/rate")
    public CommonResult<String> delRate(@RequestParam List<Integer> ids) {
        boolean b = trackRateService.removeBatchByIds(ids);
        return CommonResult.success(b?"success":"failed");
    }
    @Operation
    @DeleteMapping("/del/sub")
    public CommonResult<String> delSub(@RequestParam List<Integer> ids) {
        boolean b = subtitlesService.removeBatchByIds(ids);
        return CommonResult.success(b?"success":"failed");
    }
    @Operation(summary = "音频附件单文件上传")
    @PostMapping("/upload/annex/{trackId}")
    public CommonResult<String> uploadFile(HttpServletRequest request,
                                           @RequestPart("file")MultipartFile file,
                                           String folder,
                                           Integer fileType,
                                           @PathVariable("trackId") Integer trackId) throws IOException {
        boolean fileValid = AsmrUtil.fileValid(file, fileType);
        if (!fileValid) {
            return CommonResult.failed("文件校验失败");
        }
        Boolean flag = trackInfoService.uploadFile(request,file,folder,fileType,trackId);
        return flag ? CommonResult.success():CommonResult.failed();
    }
}
